package com.wxtl.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;

import com.wxtl.dao.base.HibernateBaseDao;
import com.wxtl.entity.Product;
import com.wxtl.util.DataGridModel;
import com.wxtl.util.Page;

/**
 * 产品Dao
 * @author liulin
 *
 */
@Repository

public class ProductDAO extends HibernateBaseDao {
	private static final Log log = LogFactory.getLog(ProductDAO.class);

	/**
	 * 获取所有产品
	 * @return
	 */
	public List<Product> getAllProduct(){
		String hql="from Product p";
		List<Product> list=getSession().createQuery(hql).list();
		return list;
	}
	
	public List<Product> query(Integer[] integers){
		StringBuilder hql=new StringBuilder();
		hql.append("from Product p where 1=1");
		if(integers!=null&&integers.length>0){
			hql.append(" and p.productId in(");
			for(int i=0;i<integers.length-1;i++){
				hql.append(integers[i]).append(",");
			}
			hql.append(integers[integers.length-1]).append(")");
		}
		return getSession().createQuery(hql.toString()).list();
	}
	
	/**
	 * 根据查询条件查询
	 * @param dgm
	 * @param product
	 * @return
	 */
	public Page query(DataGridModel dgm,Product product){
		//构建hql
		StringBuilder hql=new StringBuilder();
		List<Object> params=new ArrayList<Object>();
		hql.append("from Product p where 1=1 ");
		if(product.getName()!=null&&!"".equals(product.getName())){
			params.add("%"+product.getName()+"%");
			hql.append(" and p.name like ?");
		}
		if(product.getCode()!=null&&!"".equals(product.getCode())){
			params.add("%"+product.getCode()+"%");
			hql.append(" and p.code like ?");
		}
		
		if(product.getExVersion()!=null&&!"".equals(product.getExVersion())){
			params.add("%"+product.getExVersion()+"%");
			hql.append(" and p.exVersion like ?");
		}
		
		hql.append(" order by p.exTime desc, p.name");
		
		return super.queryForList(hql.toString(), params.toArray(new Object[]{}), dgm.getPage(), dgm.getRows());
	}
	
	public Product load(int productId){
		return (Product) getHibernateTemplate().load(Product.class, productId);
	}
	
	public void delete(int[] ids){
		super.delete("product","productId", ids);
	}
}